#include "Microservice.hpp"
#include <iostream>
#include <unordered_map> // 包含unordered_map用于保存用户信息，实际项目通常使用数据库

// 使用unordered_map来保存用户信息的简单数据库
std::unordered_map<std::string, std::string> userDatabase;

// 示例：创建用户服务
std::string createUser(const std::string& userInfo) {
    // 假设用户信息格式为 "UserID:UserName"
    std::size_t delimiterPos = userInfo.find(':');
    if (delimiterPos != std::string::npos) {
        std::string userId = userInfo.substr(0, delimiterPos);
        std::string userName = userInfo.substr(delimiterPos + 1);
        userDatabase[userId] = userName; // 将用户信息保存到内存中
        std::cout << "Creating user with ID: " << userId << " and name: " << userName << std::endl;
        std::cout << "User database size: " << userDatabase.size() << std::endl;
        // 返回保存成功的消息
        return "User created successfully";
    } else {
        return "Error: Invalid user information format";
    }
}

// 示例：获取用户信息服务
std::string getUserInfo(const std::string& userId) {
    auto it = userDatabase.find(userId);
    if (it != userDatabase.end()) {
        std::cout << "Getting user info for user with ID: " << userId << std::endl;
        // 返回相应的用户信息
        return "User info for user with ID " + userId + ": " + it->second;
    } else {
        std::cout << "User with ID " << userId << " not found" << std::endl;
        return "Error: User with ID " + userId + " not found";
    }
}

int main() {
    // 创建两个微服务实例
    Microservice createUserService("tcp://*:5555");
    Microservice getUserInfoService("tcp://*:5556");

    // 设置服务的消息处理回调函数
    createUserService.setCallback(createUser);
    getUserInfoService.setCallback(getUserInfo);

    // 启动服务
    createUserService.start();
    getUserInfoService.start();

    // 等待服务运行
    std::cout << "Services are running..." << std::endl;
    std::cin.get();

    // 停止服务
    createUserService.stop();
    getUserInfoService.stop();

    return 0;
}